package org.dromara.wmscloud.service.impl;

import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.dromara.wmscloud.domain.bo.BaseSupplierBo;
import org.dromara.wmscloud.domain.vo.BaseSupplierVo;
import org.dromara.wmscloud.domain.BaseSupplier;
import org.dromara.wmscloud.mapper.BaseSupplierMapper;
import org.dromara.wmscloud.service.IBaseSupplierService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 供应商Service业务层处理
 *
 * @author hou
 * @date 2025-04-04
 */
@RequiredArgsConstructor
@Service
public class BaseSupplierServiceImpl implements IBaseSupplierService {

    private final BaseSupplierMapper baseSupplierMapper;

    /**
     * 查询供应商
     *
     * @param supplierId 主键
     * @return 供应商
     */
    @Override
    public BaseSupplierVo queryById(Long supplierId){
        return baseSupplierMapper.selectVoById(supplierId);
    }

    /**
     * 分页查询供应商列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 供应商分页列表
     */
    @Override
    public TableDataInfo<BaseSupplierVo> queryPageList(BaseSupplierBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<BaseSupplier> lqw = buildQueryWrapper(bo);
        Page<BaseSupplierVo> result = baseSupplierMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的供应商列表
     *
     * @param bo 查询条件
     * @return 供应商列表
     */
    @Override
    public List<BaseSupplierVo> queryList(BaseSupplierBo bo) {
        LambdaQueryWrapper<BaseSupplier> lqw = buildQueryWrapper(bo);
        return baseSupplierMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<BaseSupplier> buildQueryWrapper(BaseSupplierBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<BaseSupplier> lqw = Wrappers.lambdaQuery();
        lqw.orderByAsc(BaseSupplier::getSupplierId);
        lqw.eq(StringUtils.isNotBlank(bo.getSupplierCode()), BaseSupplier::getSupplierCode, bo.getSupplierCode());
        lqw.like(StringUtils.isNotBlank(bo.getSupplierName()), BaseSupplier::getSupplierName, bo.getSupplierName());
        lqw.eq(StringUtils.isNotBlank(bo.getSupplyType()), BaseSupplier::getSupplyType, bo.getSupplyType());
        lqw.eq(StringUtils.isNotBlank(bo.getAddress()), BaseSupplier::getAddress, bo.getAddress());
        lqw.eq(StringUtils.isNotBlank(bo.getContact()), BaseSupplier::getContact, bo.getContact());
        lqw.eq(StringUtils.isNotBlank(bo.getTaxNumber()), BaseSupplier::getTaxNumber, bo.getTaxNumber());
        lqw.eq(StringUtils.isNotBlank(bo.getDepositBank()), BaseSupplier::getDepositBank, bo.getDepositBank());
        lqw.eq(StringUtils.isNotBlank(bo.getBankAccount()), BaseSupplier::getBankAccount, bo.getBankAccount());
        lqw.eq(StringUtils.isNotBlank(bo.getIsQualified()), BaseSupplier::getIsQualified, bo.getIsQualified());
        lqw.eq(StringUtils.isNotBlank(bo.getCity()), BaseSupplier::getCity, bo.getCity());
        lqw.eq(StringUtils.isNotBlank(bo.getPostalCode()), BaseSupplier::getPostalCode, bo.getPostalCode());
        return lqw;
    }

    /**
     * 新增供应商
     *
     * @param bo 供应商
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(BaseSupplierBo bo) {
        BaseSupplier add = MapstructUtils.convert(bo, BaseSupplier.class);
        validEntityBeforeSave(add);
        boolean flag = baseSupplierMapper.insert(add) > 0;
        if (flag) {
            bo.setSupplierId(add.getSupplierId());
        }
        return flag;
    }

    /**
     * 修改供应商
     *
     * @param bo 供应商
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(BaseSupplierBo bo) {
        BaseSupplier update = MapstructUtils.convert(bo, BaseSupplier.class);
        validEntityBeforeSave(update);
        return baseSupplierMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(BaseSupplier entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除供应商信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseSupplierMapper.deleteByIds(ids) > 0;
    }
}
